<?php
/**
 * Description of iExamManualclass
 *
 * @author Chisheng Lu, Jeffy Shih
 * @copyright DoRISE team, Science Education center, NTNU.
 * @since 2010-05-03
 * @version 1.1
 */
include 'ExamBasic.Class.php';
include 'ExamProcess.Class.php';

class iExamManual {
    //put your code here

    private $mysqli;
    private $objExamBasic = null;
    private $intExamId = null;
    private $aryExamBasic = null;
    private $objExamProcess = null;
    private $aryExamProcess = null;

    /**
     *
     * @param Integer $intExamId
     */
    function __construct($intExamId = 0) {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");

        if ($intExamId != 0) {
            $this->getExam($intExamId);
        }
    }

    /**
     *
     * @param Integer $intExamId
     */
    private function getExam($intExamId) {
        $this->intExamId = $intExamId;
        $this->objExamBasic = new ExamBasic();
        $this->aryExamBasic = $this->objExamBasic->getExamBasicInfoByEBId($intExamId);
        $this->objExamProcess = new ExamProcess();
        $this->aryExamProcess = $this->objExamProcess->getExamProcessInfoByEBId($intExamId, '`EPId`, `EPDoth`, `EPCategory`, `EPQEId`');
    }

    /**
     *
     * @param Integer $intEPId
     * @return Boolean
     */
    public function delExamProcessByEPId($intEPId) {
        $bolRtn = false;
        //檢查試卷程序編號是否為空白
        if (empty ($intEPId)) {
            throw new Exception('錯誤！缺少試卷程序編號。');
        }

        $bolDeleted = false;
        foreach ($this->aryExamProcess as $a_n => $aryOneExamProcess) {
            if ($bolDeleted) {
                $this->aryExamProcess[$a_n][EPDoth]--;
            } else {
                if ($aryOneExamProcess['EPId'] == $intEPId) {
                    //刪除
                    $this->objExamProcess->delExamProcess($intEPId);
                    array_splice($this->aryExamProcess, $a_n, 1);
                    $bolDeleted = true;
                }
            }
        }
        return $bolRtn;
    }

    /**
     *
     * @return Boolean
     */
    public function delExam() {
        $bolRtn = false;
        //檢查試卷基本資料編號是否為空白
        if ($this->aryExamBasic) {
            $this->objExamBasic->delExamBasic($this->intExamId);
            $this->aryExamBasic = null;
        }
        if ($this->objExamBasic) {
            $this->objExamBasic = null;
        }

        foreach ($this->aryExamProcess as $a_n => $aryOneExamProcess) {
            $this->objExamProcess->delExamProcess($aryOneExamProcess['EPId']);
        }
        $this->aryExamProcess = null;

        if ($this->objExamProcess) {
            $this->objExamProcess = null;
        }
        $this->intExamId = null;
        
        return $bolRtn;

    }

    /**
     *
     * @return Array
     */
    public function getExamBasicInfo() {
        return $this->aryExamBasic;
    }

    /**
     *
     * @return Array
     */
    public function getAllExamProcess() {
        return $this->aryExamProcess;
    }

    /**
     *
     * @param Integer $intDoth
     * @return Array
     */
    public function getExamProcessByDoth($intDoth) {
        foreach ($this->aryExamProcess as $a_n => $aryOneExamProcess) {
            if ($aryOneExamProcess['EPDoth'] == $intDoth) {
                return $aryOneExamProcess;
            }
        }
        return null;
    }

    /**
     *
     * @return Integer
     */
    public function TotalProcesses() {
        return count($this->aryExamProcess);
    }

    /**
     *
     * @return Integer
     */
    public function getTotalQuestions() {
        $intTQ = 0;
        foreach ($this->aryExamProcess as $a_n => $aryOneExamProcess) {
            if ($aryOneExamProcess['EPCategory'] == ExamProcess::EPQuestion) {
                $intTQ++;
            }
        }
        return $intTQ;
    }

    /**
     *
     * @return Integer
     */
    public function getTotalQuestionnaire() {
        $intTQ = 0;
        foreach ($this->aryExamProcess as $a_n => $aryOneExamProcess) {
            if ($aryOneExamProcess['EPCategory'] == ExamProcess::EPQuestionnaire) {
                $intTQ++;
            }
        }
        return $intTQ;
    }

    /**
     *
     * @return Integer
     */
    public function getTotalInformation() {
        $intTQ = 0;
        foreach ($this->aryExamProcess as $a_n => $aryOneExamProcess) {
            if ($aryOneExamProcess['EPCategory'] == ExamProcess::EPInformation) {
                $intTQ++;
            }
        }
        return $intTQ;
    }

    /**
     *
     * @since version 1.1 (2010-06-01)
     * @return Boolean True: Locked, False: unLocked
     */
    public function checkExamLock(){
        return $this->objExamBasic->chkExamLockByEBId($this->intExamId);
    }

    /**
     *
     * @return String
     */
    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
